package com.yhg.xinyi.movie;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yhg.xinyi.R;
import com.yhg.xinyi.bean.movieInfoBean;
import com.yhg.xinyi.utils.AssetsDatabaseManager;
import com.yhg.xinyi.utils.DBOpenHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MovieActivity extends AppCompatActivity implements View.OnClickListener{
    ImageView backIv;
    ListView movieLv;
    ImageView picIv;
    TextView nameTv,infoTv,scoreTv,ratedTv,instroductionTv;
    List<movieInfoBean> movieDatas ;
    private MovieInfoAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);
        // 初始化，只需要调用一次
        AssetsDatabaseManager.initManager(getApplicationContext());
        // 获取管理对象，因为数据库需要通过管理对象才能够获取
        AssetsDatabaseManager mg = AssetsDatabaseManager.getManager();
        // 通过管理对象获取数据库
        SQLiteDatabase movieDb = mg.getDatabase("movie.db");
        movieDatas = getDBList(movieDb);
        //初始化控件
        initPageView();
        //创建适配器
        adapter = new MovieInfoAdapter(this, movieDatas);
        movieLv.setAdapter(adapter);
//        System.out.println("运行结果"+movieDatas);
        setListener();

    }

    private void setListener() {
        movieLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                movieInfoBean infoBean = movieDatas.get(position);
                String infoLink = infoBean.getInfoLink();
                Intent intent = new Intent(MovieActivity.this,DescActivity.class);
                intent.putExtra("infoLink",infoLink);
                startActivity(intent);
            }
        });
    }

    /* 获取所有要求显示的内容的集合    "select * from movie250"        */
    public List<movieInfoBean> getDBList(SQLiteDatabase movieDb){
        List<movieInfoBean>list = new ArrayList<>();
        Cursor cursor = movieDb.query("movie250", null, null, null, null, null, null);
        while (cursor.moveToNext()) {

            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String infoLink = cursor.getString(cursor.getColumnIndex("infoLink"));
            String picLink = cursor.getString(cursor.getColumnIndex("picLink"));
            String cname = cursor.getString(cursor.getColumnIndex("cname"));
            String ename = cursor.getString(cursor.getColumnIndex("ename"));
            String score = cursor.getString(cursor.getColumnIndex("score"));
            String rated = cursor.getString(cursor.getColumnIndex("rated"));
            String instroduction = cursor.getString(cursor.getColumnIndex("instroduction"));
            String info = cursor.getString(cursor.getColumnIndex("info"));

            movieInfoBean bean = new movieInfoBean(id,infoLink,picLink,cname,ename,score,rated,instroduction,info);
            list.add(bean);
        }
        return list;
    }


    private void initPageView() {
        backIv = findViewById(R.id.title_iv_back);
        movieLv = findViewById(R.id.movieList_lv);
        picIv = findViewById(R.id.movie_pic);
        nameTv = findViewById(R.id.movie_name);
        infoTv = findViewById(R.id.movie_info);
        scoreTv = findViewById(R.id.movie_score);
        ratedTv = findViewById(R.id.movie_rated);
        instroductionTv = findViewById(R.id.movie_instroduction);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_iv_back:
                finish();
                break;
        }
    }
}